package com.cqie.hospitalescort.controller;

import com.cqie.hospitalescort.DTO.IdDTO;
import com.cqie.hospitalescort.DTO.WelfareShopDTO;
import com.cqie.hospitalescort.entity.WelfareShop;
import com.cqie.hospitalescort.model.Result;
import com.cqie.hospitalescort.service.WelfareShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gdy
 * @since 2025-02-18
 */
@RestController
@RequestMapping("/welfareShop")
@Api(value = "福利商品", tags = {"福利商品"})
public class WelfareShopController extends BaseController {

    @Autowired
    private WelfareShopService welfareShopService;

    @GetMapping("/list")
    @ApiOperation(value = "商品列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
//    @PreAuthorize(value = "hasAnyRole('ROLE_ADMIN','ROLE_ESCORT')")
    public Result<List<WelfareShop>> findAll(@RequestParam(required = false) String name) {
        return response(welfareShopService.findAll(name));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize(value = "hasRole('ROLE_ADMIN')")
    public Result<Void> addOrEditShop(@Validated @ModelAttribute WelfareShopDTO dto, @RequestParam(value = "picture") List<MultipartFile> picture) {
        welfareShopService.addOrEditShop(dto, picture);
        return response();
    }

    @GetMapping("/exchange")
    @ApiOperation(value = "兑换商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @PreAuthorize(value = "hasRole('ROLE_ESCORT')")
    public Result<Void> exchangeShop(@RequestParam Integer id, @RequestParam Integer count){
        welfareShopService.exchangeShop(id, count);
        return response();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize(value = "hasRole('ROLE_ADMIN')")
    public Result<Void> deleteShop(IdDTO dto) {
        welfareShopService.deleteShop(dto);
        return response();
    }

}
